﻿using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;

namespace CommonSLT
{
    public class CSSEManageForm : DevExpress.XtraEditors.XtraForm
    {
        #region variants
        protected string formText = "";
        protected string gridText = "";
        protected string primaryKey = "";
        protected string sortColumn = "";
        protected string orderByExpression = "ASC";
        protected string searchColumn = "";
        protected string deleteAlertColumnShow = "";
        protected string deleteAlertDesShow = "";
        protected string searchTooltip = "";
        protected string sOrgWhere = "";
        protected string sWhere = "";
        protected bool showAddButton = true;
        protected bool showCopyButton = true;
        protected bool showEditButton = true;
        protected bool showDeleteButton = true;
        protected bool showPrintButton = true;
        protected int pageNumber = 1;
        protected int pageNumbers = 0;
        //Control
        protected DevExpress.XtraEditors.PanelControl pnTop;
        protected SearchControl searchControl;
        protected DevExpress.XtraEditors.PanelControl pnBody;
        protected GridControl gridControl;
        protected DevExpress.XtraEditors.PanelControl panelControl3;
        protected DevExpress.XtraEditors.PanelControl pnBtnDelete;
        protected DevExpress.XtraEditors.SimpleButton btnDelete;
        protected DevExpress.XtraEditors.PanelControl pnBtnEdit;
        protected DevExpress.XtraEditors.SimpleButton btnEdit;
        protected DevExpress.XtraEditors.PanelControl pnBtnCopy;
        protected DevExpress.XtraEditors.SimpleButton btnCopy;
        protected DevExpress.XtraEditors.PanelControl pnBtnAdd;
        protected DevExpress.XtraEditors.SimpleButton btnAdd;
        protected DevExpress.XtraEditors.PanelControl pnBtnPrint;
        protected DevExpress.XtraEditors.SimpleButton btnPrint;
        protected DevExpress.XtraEditors.PanelControl pnBtnClose;
        protected DevExpress.XtraEditors.SimpleButton btnClose;
        protected NavControl navControl;
        #endregion variants
        //==========================================================================

        #region control
        private void InitializeComponent()
        {
            this.pnTop = new DevExpress.XtraEditors.PanelControl();
            this.searchControl = new CommonSLT.SearchControl();
            this.pnBody = new DevExpress.XtraEditors.PanelControl();
            this.gridControl = new CommonSLT.GridControl();
            this.panelControl3 = new DevExpress.XtraEditors.PanelControl();
            this.pnBtnPrint = new DevExpress.XtraEditors.PanelControl();
            this.btnPrint = new DevExpress.XtraEditors.SimpleButton();
            this.pnBtnDelete = new DevExpress.XtraEditors.PanelControl();
            this.btnDelete = new DevExpress.XtraEditors.SimpleButton();
            this.pnBtnEdit = new DevExpress.XtraEditors.PanelControl();
            this.btnEdit = new DevExpress.XtraEditors.SimpleButton();
            this.pnBtnCopy = new DevExpress.XtraEditors.PanelControl();
            this.btnCopy = new DevExpress.XtraEditors.SimpleButton();
            this.pnBtnAdd = new DevExpress.XtraEditors.PanelControl();
            this.btnAdd = new DevExpress.XtraEditors.SimpleButton();
            this.pnBtnClose = new DevExpress.XtraEditors.PanelControl();
            this.btnClose = new DevExpress.XtraEditors.SimpleButton();
            this.navControl = new CommonSLT.NavControl();
            ((System.ComponentModel.ISupportInitialize)(this.pnTop)).BeginInit();
            this.pnTop.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pnBody)).BeginInit();
            this.pnBody.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.panelControl3)).BeginInit();
            this.panelControl3.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pnBtnPrint)).BeginInit();
            this.pnBtnPrint.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pnBtnDelete)).BeginInit();
            this.pnBtnDelete.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pnBtnEdit)).BeginInit();
            this.pnBtnEdit.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pnBtnCopy)).BeginInit();
            this.pnBtnCopy.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pnBtnAdd)).BeginInit();
            this.pnBtnAdd.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pnBtnClose)).BeginInit();
            this.pnBtnClose.SuspendLayout();
            this.SuspendLayout();
            // 
            // pnTop
            // 
            this.pnTop.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
            this.pnTop.Controls.Add(this.searchControl);
            this.pnTop.Dock = System.Windows.Forms.DockStyle.Top;
            this.pnTop.Location = new System.Drawing.Point(0, 0);
            this.pnTop.Name = "pnTop";
            this.pnTop.Size = new System.Drawing.Size(731, 50);
            this.pnTop.TabIndex = 1;
            // 
            // searchControl
            // 
            this.searchControl.Dock = System.Windows.Forms.DockStyle.Fill;
            this.searchControl.Location = new System.Drawing.Point(0, 0);
            this.searchControl.Name = "searchControl";
            this.searchControl.Size = new System.Drawing.Size(731, 50);
            this.searchControl.TabIndex = 0;
            // 
            // pnBody
            // 
            this.pnBody.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
            this.pnBody.Controls.Add(this.gridControl);
            this.pnBody.Controls.Add(this.panelControl3);
            this.pnBody.Controls.Add(this.navControl);
            this.pnBody.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pnBody.Location = new System.Drawing.Point(0, 50);
            this.pnBody.Name = "pnBody";
            this.pnBody.Size = new System.Drawing.Size(731, 434);
            this.pnBody.TabIndex = 3;
            // 
            // gridControl
            // 
            this.gridControl.Dock = System.Windows.Forms.DockStyle.Fill;
            this.gridControl.Location = new System.Drawing.Point(0, 32);
            this.gridControl.Name = "gridControl";
            this.gridControl.Size = new System.Drawing.Size(731, 373);
            this.gridControl.TabIndex = 3;
            // 
            // panelControl3
            // 
            this.panelControl3.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
            this.panelControl3.Controls.Add(this.pnBtnPrint);
            this.panelControl3.Controls.Add(this.pnBtnDelete);
            this.panelControl3.Controls.Add(this.pnBtnEdit);
            this.panelControl3.Controls.Add(this.pnBtnCopy);
            this.panelControl3.Controls.Add(this.pnBtnAdd);
            this.panelControl3.Controls.Add(this.pnBtnClose);
            this.panelControl3.Dock = System.Windows.Forms.DockStyle.Top;
            this.panelControl3.Location = new System.Drawing.Point(0, 0);
            this.panelControl3.Name = "panelControl3";
            this.panelControl3.Size = new System.Drawing.Size(731, 32);
            this.panelControl3.TabIndex = 2;
            // 
            // pnBtnPrint
            // 
            this.pnBtnPrint.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
            this.pnBtnPrint.Controls.Add(this.btnPrint);
            this.pnBtnPrint.Dock = System.Windows.Forms.DockStyle.Right;
            this.pnBtnPrint.Location = new System.Drawing.Point(557, 0);
            this.pnBtnPrint.Name = "pnBtnPrint";
            this.pnBtnPrint.Padding = new System.Windows.Forms.Padding(3);
            this.pnBtnPrint.Size = new System.Drawing.Size(87, 32);
            this.pnBtnPrint.TabIndex = 6;
            // 
            // btnPrint
            // 
            this.btnPrint.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btnPrint.Image = global::CommonSLT.Properties.Resources.Print;
            this.btnPrint.Location = new System.Drawing.Point(3, 3);
            this.btnPrint.Name = "btnPrint";
            this.btnPrint.Size = new System.Drawing.Size(81, 26);
            this.btnPrint.TabIndex = 1;
            this.btnPrint.Text = "Xem && In";
            // 
            // pnBtnDelete
            // 
            this.pnBtnDelete.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
            this.pnBtnDelete.Controls.Add(this.btnDelete);
            this.pnBtnDelete.Dock = System.Windows.Forms.DockStyle.Left;
            this.pnBtnDelete.Location = new System.Drawing.Point(261, 0);
            this.pnBtnDelete.Name = "pnBtnDelete";
            this.pnBtnDelete.Padding = new System.Windows.Forms.Padding(3);
            this.pnBtnDelete.Size = new System.Drawing.Size(87, 32);
            this.pnBtnDelete.TabIndex = 5;
            // 
            // btnDelete
            // 
            this.btnDelete.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btnDelete.Image = global::CommonSLT.Properties.Resources.Delete;
            this.btnDelete.Location = new System.Drawing.Point(3, 3);
            this.btnDelete.Name = "btnDelete";
            this.btnDelete.Size = new System.Drawing.Size(81, 26);
            this.btnDelete.TabIndex = 1;
            this.btnDelete.Text = "&Xóa";
            // 
            // pnBtnEdit
            // 
            this.pnBtnEdit.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
            this.pnBtnEdit.Controls.Add(this.btnEdit);
            this.pnBtnEdit.Dock = System.Windows.Forms.DockStyle.Left;
            this.pnBtnEdit.Location = new System.Drawing.Point(174, 0);
            this.pnBtnEdit.Name = "pnBtnEdit";
            this.pnBtnEdit.Padding = new System.Windows.Forms.Padding(3);
            this.pnBtnEdit.Size = new System.Drawing.Size(87, 32);
            this.pnBtnEdit.TabIndex = 4;
            // 
            // btnEdit
            // 
            this.btnEdit.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btnEdit.Image = global::CommonSLT.Properties.Resources.Edit;
            this.btnEdit.Location = new System.Drawing.Point(3, 3);
            this.btnEdit.Name = "btnEdit";
            this.btnEdit.Size = new System.Drawing.Size(81, 26);
            this.btnEdit.TabIndex = 1;
            this.btnEdit.Text = "&Sửa";
            // 
            // pnBtnCopy
            // 
            this.pnBtnCopy.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
            this.pnBtnCopy.Controls.Add(this.btnCopy);
            this.pnBtnCopy.Dock = System.Windows.Forms.DockStyle.Left;
            this.pnBtnCopy.Location = new System.Drawing.Point(87, 0);
            this.pnBtnCopy.Name = "pnBtnCopy";
            this.pnBtnCopy.Padding = new System.Windows.Forms.Padding(3);
            this.pnBtnCopy.Size = new System.Drawing.Size(87, 32);
            this.pnBtnCopy.TabIndex = 3;
            // 
            // btnCopy
            // 
            this.btnCopy.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btnCopy.Image = global::CommonSLT.Properties.Resources.Copy;
            this.btnCopy.Location = new System.Drawing.Point(3, 3);
            this.btnCopy.Name = "btnCopy";
            this.btnCopy.Size = new System.Drawing.Size(81, 26);
            this.btnCopy.TabIndex = 1;
            this.btnCopy.Text = "Sao C&hép";
            // 
            // pnBtnAdd
            // 
            this.pnBtnAdd.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
            this.pnBtnAdd.Controls.Add(this.btnAdd);
            this.pnBtnAdd.Dock = System.Windows.Forms.DockStyle.Left;
            this.pnBtnAdd.Location = new System.Drawing.Point(0, 0);
            this.pnBtnAdd.Name = "pnBtnAdd";
            this.pnBtnAdd.Padding = new System.Windows.Forms.Padding(3);
            this.pnBtnAdd.Size = new System.Drawing.Size(87, 32);
            this.pnBtnAdd.TabIndex = 2;
            // 
            // btnAdd
            // 
            this.btnAdd.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btnAdd.Image = global::CommonSLT.Properties.Resources.Add;
            this.btnAdd.Location = new System.Drawing.Point(3, 3);
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Size = new System.Drawing.Size(81, 26);
            this.btnAdd.TabIndex = 1;
            this.btnAdd.Text = "&Thêm";
            // 
            // pnBtnClose
            // 
            this.pnBtnClose.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
            this.pnBtnClose.Controls.Add(this.btnClose);
            this.pnBtnClose.Dock = System.Windows.Forms.DockStyle.Right;
            this.pnBtnClose.Location = new System.Drawing.Point(644, 0);
            this.pnBtnClose.Name = "pnBtnClose";
            this.pnBtnClose.Padding = new System.Windows.Forms.Padding(3);
            this.pnBtnClose.Size = new System.Drawing.Size(87, 32);
            this.pnBtnClose.TabIndex = 7;
            // 
            // btnClose
            // 
            this.btnClose.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btnClose.Image = global::CommonSLT.Properties.Resources.Exit;
            this.btnClose.Location = new System.Drawing.Point(3, 3);
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new System.Drawing.Size(81, 26);
            this.btnClose.TabIndex = 0;
            this.btnClose.Text = "Đóng";
            // 
            // navControl
            // 
            this.navControl.Appearance.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.navControl.Appearance.Options.UseFont = true;
            this.navControl.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.navControl.Location = new System.Drawing.Point(0, 405);
            this.navControl.Name = "navControl";
            this.navControl.Size = new System.Drawing.Size(731, 29);
            this.navControl.TabIndex = 1;
            // 
            // CSSEManageForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.AutoValidate = System.Windows.Forms.AutoValidate.Disable;
            this.ClientSize = new System.Drawing.Size(731, 484);
            this.Controls.Add(this.pnBody);
            this.Controls.Add(this.pnTop);
            this.Name = "CSSEManageForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "CSSEManageForm";
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            ((System.ComponentModel.ISupportInitialize)(this.pnTop)).EndInit();
            this.pnTop.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.pnBody)).EndInit();
            this.pnBody.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.panelControl3)).EndInit();
            this.panelControl3.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.pnBtnPrint)).EndInit();
            this.pnBtnPrint.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.pnBtnDelete)).EndInit();
            this.pnBtnDelete.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.pnBtnEdit)).EndInit();
            this.pnBtnEdit.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.pnBtnCopy)).EndInit();
            this.pnBtnCopy.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.pnBtnAdd)).EndInit();
            this.pnBtnAdd.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.pnBtnClose)).EndInit();
            this.pnBtnClose.ResumeLayout(false);
            this.ResumeLayout(false);

        }
        #endregion control
        //==========================================================================

        #region form
        public CSSEManageForm()
        {
            InitializeComponent();
            this.Load += new EventHandler(CSSEManageForm_Load);
            this.Shown += new EventHandler(CSSEManageForm_Shown);
            this.FormClosed += new FormClosedEventHandler(CSSEManageForm_FormClosed);
            this.KeyPreview = true;
            this.KeyDown += new KeyEventHandler(CSSEManageForm_KeyDown);
            //
            this.searchControl.OnSearchContent_KeyDown += new KeyEventHandler(searchControl_OnSearchContent_KeyDown);
            this.searchControl.OnBtnClear_Click += new EventHandler<EventArgs>(searchControl_OnBtnClear_Click);
            this.searchControl.OnBtnSearch_Click += new EventHandler<EventArgs>(searchControl_OnBtnSearch_Click);
            this.searchControl.OnBtnRefresh_Click += new EventHandler<EventArgs>(searchControl_OnBtnRefresh_Click);
            //
            this.gridControl.OnEmptyArea_DoubleClick += new EventHandler<EventArgs>(gridControl_OnEmptyArea_DoubleClick);
            this.gridControl.OnFocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(gridControl_OnFocusedRowChanged);
            this.gridControl.OnInColumn_Click += new EventHandler<EventArgs>(gridControl_OnInColumn_Click);
            this.gridControl.OnRow_DoubleClick += new EventHandler<EventArgs>(gridControl_OnRow_DoubleClick);
            //
            this.navControl.OnNavigate += new MyNavigatorEventHandler(navControl_OnNavigate);
            //
            this.btnAdd.Click += new EventHandler(btnAdd_Click);
            this.btnCopy.Click += new EventHandler(btnCopy_Click);
            this.btnEdit.Click += new EventHandler(btnEdit_Click);
            this.btnDelete.Click += new EventHandler(btnDelete_Click);
            this.btnPrint.Click += new EventHandler(btnPrint_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);
        }

        #region form events
        private void CSSEManageForm_Load(object sender, EventArgs e)
        {
            InitForm();
            //
            this.Text = this.formText;
            //
            this.searchControl.SetTooltip(this.searchTooltip);
            //
            this.pnBtnAdd.Visible = showAddButton;
            this.pnBtnCopy.Visible = showCopyButton;
            this.pnBtnEdit.Visible = showEditButton;
            this.pnBtnDelete.Visible = showDeleteButton;
            this.pnBtnPrint.Visible = showPrintButton;
            //
            this.gridControl.sortColumn = this.sortColumn;
            this.gridControl.orderByExpression = this.orderByExpression;
            InitGrid();
            //
            this.gridControl.sortColumn = this.sortColumn;
            this.gridControl.SetViewCaption(this.gridText);
            //
            Search(true);
        }

        private void CSSEManageForm_Shown(object sender, EventArgs e)
        {
        }

        private void CSSEManageForm_FormClosed(object sender, FormClosedEventArgs e)
        {
        }

        private void CSSEManageForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                Close_Click();
            else
                if (e.KeyCode == Keys.T && Control.ModifierKeys == Keys.Control)
                    Add_Click();
                else
                    if (e.KeyCode == Keys.H && Control.ModifierKeys == Keys.Control)
                        Copy_Click();
                    else
                        if (e.KeyCode == Keys.S && Control.ModifierKeys == Keys.Control)
                            Edit_Click();
                        else
                            if (e.KeyCode == Keys.X && Control.ModifierKeys == Keys.Control)
                                Delete_Click();
                            else
                                if (e.KeyCode == Keys.F && Control.ModifierKeys == Keys.Control)
                                    searchControl.SetFocusToSearchContent();
                                else
                                    if (e.KeyCode == Keys.P && Control.ModifierKeys == Keys.Control)
                                        Print_Click();
                                    else
                                        if (e.KeyCode == Keys.F5)
                                            Search(false);
        }
        #endregion form events
        #endregion form
        //==========================================================================

        #region virtual functions
        protected virtual void InitForm()
        {
        }

        protected virtual void InitGrid()
        {
        }

        protected virtual void InitData()
        {
        }

        protected virtual void GetWhere(bool getNew)
        {
            if (getNew)
            {
                this.sWhere = "";
                if (this.searchColumn.Length > 0)
                {
                    string[] arrSearchColumns = this.searchColumn.Split('|');
                    int i = 0;
                    if (this.searchControl.GetSearchContent().Length > 0)
                        for (i = 0; i < arrSearchColumns.Length; i++)
                            this.sWhere += " OR " + arrSearchColumns[i] + " LIKE N'%" + this.searchControl.GetSearchContent() + "%'";

                    if (this.sWhere.StartsWith(" OR"))
                        this.sWhere = this.sWhere.Remove(0, 3);
                }
                if (this.sOrgWhere != "")
                    this.sWhere = (this.sWhere != "" ? this.sOrgWhere + " AND (" + this.sWhere + ")" : this.sOrgWhere);
            }
        }

        protected virtual object GetData()
        {
            return null;
        }

        protected virtual void SearchContent_KeyDown(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                Search(true);
        }

        protected virtual void BtnClear_Click()
        {
            searchControl.SetSearchContent(null);
            Search(true);
        }

        protected virtual void BtnSearch_Click()
        {
            Search(true);
        }

        protected virtual void BtnRefresh_Click()
        {
            Search(false);
        }

        protected virtual void SetButtonStatus()
        {
            bool enableButton = this.gridControl.GetRowCount() > 0;
            this.btnCopy.Enabled = enableButton;
            this.btnEdit.Enabled = enableButton;
            this.btnDelete.Enabled = enableButton;
        }

        protected virtual void EmptyArea_DoubleClick()
        {
            Add_Click();
        }

        protected virtual void FocusedRowChanged(DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
        }

        protected virtual void InColumn_Click()
        {
            Search(false);
        }

        protected virtual void Row_DoubleClick()
        {
            Edit_Click();
        }

        protected virtual void AddAction()
        {

        }

        protected virtual void CopyAction(object selectedID)
        {
        }

        protected virtual void EditAction(object selectedID)
        {
        }

        protected virtual void DeleteAction(object selectedID)
        {
        }

        protected virtual void PrintAction()
        {
        }

        protected virtual void CloseAction()
        {
            this.Close();
        }

        protected virtual void frmDetail_OnSaveCompleted(object sender, OnSaveCompletedEventArgs e)
        {
            Search(false);
        }

        protected virtual void frm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Utility.Utility.SetDisbaleForm(this, false);
        }

        protected virtual void Search(bool getNew)
        {
            if (getNew)
                this.pageNumber = 1;
            GetWhere(getNew);
            InitData();
            gridControl.SetDataSource(GetData());
            SetTotalRow();
            SetButtonStatus();
        }
        #endregion virtual functions
        //==========================================================================

        #region private functions
        private void Add_Click()
        {
            try
            {
                if (this.btnAdd.Enabled && this.showAddButton)
                {
                    Utility.Utility.SetDisbaleForm(this, true);
                    AddAction();
                }
            }
            catch
            {
                Utility.Utility.SetDisbaleForm(this, false);
            }
        }

        private void Copy_Click()
        {
            try
            {
                if (this.btnCopy.Enabled && this.showCopyButton)
                {
                    Utility.Utility.SetDisbaleForm(this, true);
                    CopyAction(this.gridControl.GetFocusedRowCellValue(this.primaryKey));
                }
            }
            catch
            {
                Utility.Utility.SetDisbaleForm(this, false);
            }
        }

        private void Edit_Click()
        {
            try
            {
                if (this.btnEdit.Enabled && this.showEditButton)
                {
                    Utility.Utility.SetDisbaleForm(this, true);
                    EditAction(this.gridControl.GetFocusedRowCellValue(this.primaryKey));
                }
            }
            catch
            {
                Utility.Utility.SetDisbaleForm(this, false);
            }
        }

        private void Delete_Click()
        {
            try
            {
                if (this.btnDelete.Enabled && this.showDeleteButton)
                {
                    Utility.Utility.SetDisbaleForm(this, true);
                    MyDialogResult dgResult = MyDialogResult.No;
                    string deleteMessage = "Bạn có chắc là muốn xóa " + this.deleteAlertDesShow + ": " + this.gridControl.GetFocusedDisplayText(this.deleteAlertColumnShow) + "?";
                    if (!this.gridControl.hasCheckColumn)
                    {
                        dgResult = MyDialog.Show(deleteMessage, "Xóa " + this.deleteAlertDesShow, MyDialogButtons.YesNo, MyDialogFocus.No, MyDialogIcon.Question);
                        if (dgResult == MyDialogResult.Yes)
                        {
                            DeleteAction(this.gridControl.GetFocusedRowCellValue(this.primaryKey));
                            this.gridControl.DeleteRow(this.gridControl.GetSelectedRows()[0]);
                        }
                    }
                    else
                    {
                        int[] rowHandles = this.gridControl.GetGridRowHandleSelected();
                        if (rowHandles != null)
                        {
                            if (rowHandles.Length > 1)
                                deleteMessage = "Xóa " + rowHandles.Length.ToString() + " " + this.deleteAlertDesShow + "?";
                            dgResult = MyDialog.Show(deleteMessage, "Xóa " + this.deleteAlertDesShow, MyDialogButtons.YesNo, MyDialogFocus.No, MyDialogIcon.Question);
                            if (dgResult == MyDialogResult.Yes)
                            {
                                foreach (int rowHandle in rowHandles)
                                    DeleteAction(this.gridControl.GetRowCellValue(rowHandle, this.primaryKey));
                                this.gridControl.DeleteRows(rowHandles);
                            }
                        }
                    }
                }
            }
            catch { }
            Utility.Utility.SetDisbaleForm(this, false);
        }

        private void Print_Click()
        {
            if (this.btnPrint.Enabled && this.showPrintButton)
                PrintAction();
        }

        private void Close_Click()
        {
            CloseAction();
        }

        private void SetTotalRow()
        {
            this.navControl.SetInfo(this.pageNumber, this.pageNumbers);
        }
        #endregion private functions
        //==========================================================================

        #region protected functions
        protected void SetEventOnSaveCompleted(CSSEDetailForm frm)
        {
            frm.OnSaveCompleted += new OnSaveCompletedEventHandler(frmDetail_OnSaveCompleted);
            frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
        }
        #endregion protected functions
        //==========================================================================

        #region events
        private void searchControl_OnSearchContent_KeyDown(object sender, KeyEventArgs e)
        {
            SearchContent_KeyDown(e);
        }

        private void searchControl_OnBtnClear_Click(object sender, EventArgs e)
        {
            BtnClear_Click();
        }

        private void searchControl_OnBtnSearch_Click(object sender, EventArgs e)
        {
            BtnSearch_Click();
        }

        private void searchControl_OnBtnRefresh_Click(object sender, EventArgs e)
        {
            BtnRefresh_Click();
        }

        private void gridControl_OnEmptyArea_DoubleClick(object sender, EventArgs e)
        {
            EmptyArea_DoubleClick();
        }

        private void gridControl_OnFocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            FocusedRowChanged(e);
        }

        private void gridControl_OnInColumn_Click(object sender, EventArgs e)
        {
            InColumn_Click();
        }

        private void gridControl_OnRow_DoubleClick(object sender, EventArgs e)
        {
            Row_DoubleClick();
        }

        private void navControl_OnNavigate(object sender, MyNavigatorEventArgs e)
        {
            this.pageNumber = e.currIndex;
            Search(false);
            this.navControl.SetInfo(e.currIndex);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Add_Click();
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            Copy_Click();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Edit_Click();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            Delete_Click();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            Print_Click();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close_Click();
        }
        #endregion events
        //==========================================================================
    }
}